package com.yunshan.welfare.service.impl;

import com.yunshan.common.utils.DateUtils;
import com.yunshan.common.utils.SecurityUtils;
import com.yunshan.common.utils.StringUtils;
import com.yunshan.common.utils.uuid.UUID;
import com.yunshan.member.domain.MemberSubCardInfo;
import com.yunshan.member.domain.MemberUserInfo;
import com.yunshan.member.service.IMemberSubCardInfoService;
import com.yunshan.member.service.IMemberUserInfoService;
import com.yunshan.welfare.domain.WelfareCoupon;
import com.yunshan.welfare.domain.WelfareCouponRecord;
import com.yunshan.welfare.domain.WelfareGiftRecord;
import com.yunshan.welfare.mapper.WelfareCouponRecordMapper;
import com.yunshan.welfare.service.IWelfareCouponRecordService;
import com.yunshan.welfare.service.IWelfareCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * 优惠券发放详情Service业务层处理
 *
 * @author yunshan
 * @date 2025-01-20
 */
@Service
public class WelfareCouponRecordServiceImpl implements IWelfareCouponRecordService
{
    @Autowired
    private WelfareCouponRecordMapper welfareCouponRecordMapper;
    @Autowired
    private IMemberUserInfoService memberUserInfoService;
    @Autowired
    private IWelfareCouponService welfareCouponService;
    @Autowired
    private IMemberSubCardInfoService memberSubCardInfoService;

    /**
     * 查询优惠券发放详情
     *
     * @param id 优惠券发放详情主键
     * @return 优惠券发放详情
     */
    @Override
    public WelfareCouponRecord selectWelfareCouponRecordById(Long id)
    {
        return welfareCouponRecordMapper.selectWelfareCouponRecordById(id);
    }

    /**
     * 查询优惠券发放详情列表
     *
     * @param welfareCouponRecord 优惠券发放详情
     * @return 优惠券发放详情
     */
    @Override
    public List<WelfareCouponRecord> selectWelfareCouponRecordList(WelfareCouponRecord welfareCouponRecord)
    {
        //副卡则查询主卡的礼包
        if (welfareCouponRecord.getMemberId() != null){
            MemberUserInfo memberUserInfo = memberUserInfoService.selectMemberUserInfoById(welfareCouponRecord.getMemberId());
            if (memberUserInfo == null){
                MemberSubCardInfo memberSubCardInfo = memberSubCardInfoService.selectMemberSubCardInfoById(welfareCouponRecord.getMemberId());
                if (memberSubCardInfo != null) {
                    welfareCouponRecord.setMemberId(memberSubCardInfo.getMemberId());
                }
            }
        }

        List<WelfareCouponRecord> welfareCouponRecords = welfareCouponRecordMapper.selectWelfareCouponRecordList(welfareCouponRecord);
        if (welfareCouponRecords.size() > 0){
            for (WelfareCouponRecord couponRecord : welfareCouponRecords) {
                WelfareCoupon welfareCoupon = welfareCouponService.selectWelfareCouponById(couponRecord.getCouponId());
                if(welfareCoupon != null && StringUtils.isNotEmpty(welfareCoupon.getName())){
                    couponRecord.setCouponName(welfareCoupon.getName());
                    couponRecord.setCouponImg(welfareCoupon.getCouponImg());
                    couponRecord.setPrice(welfareCoupon.getPrice());
                }
            }
        }

        return welfareCouponRecords;
    }

    /**
     * 新增优惠券发放详情
     *
     * @param welfareCouponRecord 优惠券发放详情
     * @return 结果
     */
    @Override
    public List<String> insertWelfareCouponRecord(WelfareCouponRecord welfareCouponRecord, List<Long> ids)
    {
        List<String> result = new ArrayList<>();

        welfareCouponRecord.setEndTime(DateUtils.toEndTime(welfareCouponRecord.getEndTimes()));
        WelfareCoupon welfareCoupon = welfareCouponService.selectWelfareCouponById(welfareCouponRecord.getCouponId());
        welfareCouponRecord.setServeType(welfareCoupon.getServeType());

        for (Long id : ids) {
            MemberUserInfo memberUserInfo = memberUserInfoService.selectMemberUserInfoById(id);
            if (memberUserInfo == null){
                throw new RuntimeException(id + "人员信息有误，发放失败！");
            }
            welfareCouponRecord.setMemberId(id);
            welfareCouponRecord.setName(memberUserInfo.getName());
            welfareCouponRecord.setMemberNum(memberUserInfo.getMemberNum());
            welfareCouponRecord.setPhone(memberUserInfo.getPhone());
            welfareCouponRecord.setMemberType(memberUserInfo.getMemberType());
            welfareCouponRecord.setCreateTime(DateUtils.getNowDate());

            //生成核销码
            // 生成一个6位的随机数并验证是否存在
            Random random = new Random();
            int randomNumber = 0;
            int num = 1;
            while (num > 0) {
                randomNumber = 100000 + random.nextInt(900000); // 生成6位随机数
                num = welfareCouponRecordMapper.verifyCouponCode(randomNumber);
            }
            welfareCouponRecord.setCouponCode(String.valueOf(randomNumber));

            if (welfareCouponRecord.getNum() == null){
                welfareCouponRecord.setNum(1);
            }
            for (int i = 0; i < welfareCouponRecord.getNum(); i++) {
                int j = welfareCouponRecordMapper.insertWelfareCouponRecord(welfareCouponRecord);
                if (j < 1){
                    throw new RuntimeException("发放失败，请稍后重试");
                }
            }

        }
        return result;
    }

    /**
     * 修改优惠券发放详情
     *
     * @param welfareCouponRecord 优惠券发放详情
     * @return 结果
     */
    @Override
    public int updateWelfareCouponRecord(WelfareCouponRecord welfareCouponRecord)
    {
        welfareCouponRecord.setUpdateTime(DateUtils.getNowDate());
        return welfareCouponRecordMapper.updateWelfareCouponRecord(welfareCouponRecord);
    }

    /**
     * 批量删除优惠券发放详情
     *
     * @param ids 需要删除的优惠券发放详情主键
     * @return 结果
     */
    @Override
    public int deleteWelfareCouponRecordByIds(Long[] ids)
    {
        return welfareCouponRecordMapper.deleteWelfareCouponRecordByIds(ids);
    }

    /**
     * 删除优惠券发放详情信息
     *
     * @param id 优惠券发放详情主键
     * @return 结果
     */
    @Override
    public int deleteWelfareCouponRecordById(Long id)
    {
        return welfareCouponRecordMapper.deleteWelfareCouponRecordById(id);
    }

    @Override
    public int batchInsertCouponRecords(Long couponId, List<MemberUserInfo> users, String endTimes ,Integer num) {

        Date endTime = DateUtils.toEndTime(endTimes);
        WelfareCoupon welfareCoupon = welfareCouponService.selectWelfareCouponById(couponId);

        WelfareCouponRecord record = new WelfareCouponRecord();
        record.setServeType(welfareCoupon.getServeType());
        record.setCreateTime(new Date());
        record.setCreateBy(SecurityUtils.getUsername());
        record.setEndTime(endTime);

        for (MemberUserInfo user : users) {
            //查询会员
            MemberUserInfo memberUserInfo = memberUserInfoService.selectMemberUserInfoByPhone(user.getPhone());
            if (memberUserInfo == null){
                throw new RuntimeException(user.getName() + "信息有误，发券失败！");
            }

            record.setMemberId(memberUserInfo.getId());
            record.setMemberNum(memberUserInfo.getMemberNum());
            record.setCouponId(couponId);
            record.setMemberType(memberUserInfo.getMemberType());
            record.setName(user.getName());
            record.setPhone(user.getPhone());

            for (int i = 0; i < num; i++) {
                welfareCouponRecordMapper.insertWelfareCouponRecord(record);
            }

        }
        return 1;
    }

    @Override
    public int updateWelfareCouponRecordStatus() {
        return welfareCouponRecordMapper.updateWelfareCouponRecordStatus();
    }

    @Override
    public List<WelfareCouponRecord> selectWelfareCouponSubList(WelfareCouponRecord welfareCouponRecord) {
        return welfareCouponRecordMapper.selectWelfareCouponSubList(welfareCouponRecord);
    }

    @Override
    public List<WelfareCouponRecord> selectWelfareCouponRecordQuery(WelfareCouponRecord welfareCouponRecord) {
        //副卡则查询主卡的礼包
        if (welfareCouponRecord.getMemberId() != null){
            MemberUserInfo memberUserInfo = memberUserInfoService.selectMemberUserInfoById(welfareCouponRecord.getMemberId());
            if (memberUserInfo == null){
                MemberSubCardInfo memberSubCardInfo = memberSubCardInfoService.selectMemberSubCardInfoById(welfareCouponRecord.getMemberId());
                if (memberSubCardInfo != null) {
                    welfareCouponRecord.setMemberId(memberSubCardInfo.getMemberId());
                }
            }
        }

        List<WelfareCouponRecord> welfareCouponRecords = welfareCouponRecordMapper.selectWelfareCouponRecordQuery(welfareCouponRecord);
        if (welfareCouponRecords.size() > 0){
            for (WelfareCouponRecord couponRecord : welfareCouponRecords) {
                WelfareCoupon welfareCoupon = welfareCouponService.selectWelfareCouponById(couponRecord.getCouponId());
                if(welfareCoupon != null && StringUtils.isNotEmpty(welfareCoupon.getName())){
                    couponRecord.setCouponName(welfareCoupon.getName());
                    couponRecord.setCouponImg(welfareCoupon.getCouponImg());
                    couponRecord.setPrice(welfareCoupon.getPrice());
                }
            }
        }

        return welfareCouponRecords;
    }


    @Override
    public Integer selectWelfareCouponRecordCount(WelfareCouponRecord welfareCouponRecord) {
        return welfareCouponRecordMapper.selectWelfareCouponRecordCount(welfareCouponRecord);
    }
}
